package cn.wolfcode.wolf2w.business.controller;

import cn.wolfcode.wolf2w.business.api.domain.*;
import cn.wolfcode.wolf2w.business.query.SearchQuery;
import cn.wolfcode.wolf2w.business.service.ISearchService;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.member.api.domain.UserInfo;
import org.elasticsearch.search.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationTargetException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

@RestController
public class SearchController {
    @Autowired
    private ISearchService searchService;

    @GetMapping("/search")
    public R<?> search(SearchQuery qo) throws InvocationTargetException, IllegalAccessException {
        System.err.println(qo.getKeyword());
        //如果前端传递中文使用了Url
        //String kw = URLDecoder.decode(qo.getKeyword(),"utf-8");

        switch (qo.getType()) {
            case 1:
                return R.ok(searchStrategy(qo));
            case 2:
                return R.ok(searchNote(qo));
            case 3:
                return R.ok(searchUserinfo(qo));
            case 4:
                return R.ok(searchAnswer(qo));
            default:
                return R.ok(searchAll(qo));
        }
    }

    private Object searchAll(SearchQuery qo) throws InvocationTargetException, IllegalAccessException {
        Page<UserInfo> userInfoPage = searchService.SearchQthHighLight(UserInfo.class, UserinfoEs.class, qo, "info", "nickname", "city");
        Page<Note> notePage = searchService.SearchQthHighLight(Note.class, NoteEs.class, qo, "title", "summary");
        Page<Strategy> strategyPage = searchService.SearchQthHighLight(Strategy.class, StrategyEs.class, qo, "title","subTitle","summary");
        Page<Destination> destinationPage = searchService.SearchQthHighLight(Destination.class,DestinationEs.class,qo,"name","info");
        long total = userInfoPage.getTotalElements()+notePage.getTotalElements()+strategyPage.getTotalElements()+destinationPage.getTotalElements();

        Map map = new HashMap();
        map.put("users",userInfoPage.getContent());
        map.put("notes",notePage.getContent());
        map.put("strategies",strategyPage.getContent());
        map.put("dests",destinationPage.getContent());
        map.put("total",total);
        return map;
    }

    private Object searchAnswer(SearchQuery qo) {

        return null;
    }

    private Object searchUserinfo(SearchQuery qo) throws InvocationTargetException, IllegalAccessException {
        Page<UserInfo> userInfos = searchService.SearchQthHighLight(UserInfo.class, UserinfoEs.class, qo, "info", "nickname", "city");
        return userInfos;
    }

    private Object searchNote(SearchQuery qo) throws InvocationTargetException, IllegalAccessException {
        Page<Note> notes = searchService.SearchQthHighLight(Note.class, NoteEs.class, qo, "title", "summary");
        return notes;
    }

    private Object searchStrategy(SearchQuery qo) throws InvocationTargetException, IllegalAccessException {
        Page<Strategy> title = searchService.SearchQthHighLight(Strategy.class, StrategyEs.class, qo, "title","subTitle","summary");
        return title;
    }
}
